'use strict';


App.controller('resourceController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
	$scope.resourceQuery = {};
	$scope.customerId =	$rootScope.user.sourceId;// 根据登录用户获得客户id
	if($scope.customerId == '' || $scope.customerId == null){// 没有客户信息，需要重新登录
		$state.go('login', null);
		Page.notice("获取登录信息失败!");
		return;
	}
	var param = {};
	param.customerId = $scope.customerId;
	/*Page.ajaxPost('customer/getContracts4Customer.do', param, function (resp) {
		 $scope.contracts = resp.data.contracts;
	 });*/
	
	$scope.resourceQuery.customerId = $scope.customerId;
	$scope.resourceQuery.resourceParentType = 1;// 默认查询车辆资源
	Page.ajaxPost('mediaType/getMediaTypesByParentId.do', {"parentId":$scope.resourceQuery.resourceParentType}, function(resp) {
		$scope.resourceQuery.resourceType="";
		$scope.resourceTypes=resp.data;
	});
	 
	$scope.resource = {
			query : function() {
				$scope.resourceQuery.pageIndex = 0;
				$scope.resourceQuery.pageSize = 10;
				Page.ajaxPost('www/bid//findCustomerResource.do', $scope.resourceQuery, function (resp) {
					$scope.dataArray = resp.data.dataArray;
					
					// 调用分页
			    	$scope.totalPages = resp.data.totalPages;
					laypage({
						cont : 'page_bar',
						pages : $scope.totalPages,// 得到总页数
						skin : '#3E97E4',
						skip : true, // 是否开启跳页
						prev : '<', // 若不显示，设置false即可
						next : '>', // 若不显示，设置false即可
						groups : 5, //连续分页数
						curr: 1, //初始化当前页
						jump : function(obj,first) {// 触发分页后的回调
							/*obj是一个object类型。包括了分页的所有配置信息。
							 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
							if(!first){
								param.pageIndex = obj.curr - 1;
								Page.ajaxPost('www/bid//findCustomerResource.do', param, function(resp) {
									$scope.dataArray = resp.data.dataArray;
									$scope.totalPages = resp.data.totalPages;
								});
							}
						}
					})
				});
			},
			//选择资源类型
			parentTypeChange : function(parentId) {
				var param = {};
				if(parentId==''){
					$scope.resourceTypes=[];
				}else {
					$scope.resourceQuery.resourceType="";
					$scope.resourceQuery.resourceParentType=parentId;
					param.parentId = parentId;
					Page.ajaxPost('mediaType/getMediaTypesByParentId.do', param, function(resp) {
						$scope.resourceQuery.resourceType="";
						$scope.resourceTypes=resp.data;
					});
				 this.query();
				}
			},
			reset : function(){
				$scope.resourceQuery.resourceType="";
				//$scope.resourceQuery.resourceParentType="";
				$scope.resourceQuery.contractId="";
				$scope.resourceQuery.resourceCode="";
			}
	}
	
	$scope.resource.query();
	
}]);